package com.das.mechanic_base.adapter.processnew;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.processsive.ProcessNewBean;

import java.util.ArrayList;
import java.util.List;

public class ConstructionProcessPlanAdapter extends RecyclerView.Adapter<ConstructionProcessPlanAdapter.ConstructionProcessPlanViewHolder> {

    private   List<ProcessNewBean.ServiceBaseShortBean>  mList;

    private Context mContext;

    public ConstructionProcessPlanAdapter(List<ProcessNewBean.ServiceBaseShortBean>  mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;

        List<ProcessNewBean.ServiceBaseShortBean> shortBeanList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            if (!mList.get(i).selected) {
                continue;
            }
            shortBeanList.add(mList.get(i));
        }

        if (iOnClickChoice != null) {
            iOnClickChoice.iOnClickChoiceContent(shortBeanList);
        }
    }

    @NonNull
    @Override
    public ConstructionProcessPlanAdapter.ConstructionProcessPlanViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ConstructionProcessPlanAdapter.ConstructionProcessPlanViewHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_car_choice_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ConstructionProcessPlanAdapter.ConstructionProcessPlanViewHolder holder, int position) {
        holder.tv_content.setText(mList.get(position).serviceBaseName);
        if (mList.get(position).selected) {
            holder.tv_content.setTextColor(Color.parseColor("#0077FF"));
            holder.tv_content.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_bg_choice_plan));
        }else {
            holder.tv_content.setTextColor(Color.parseColor("#454957"));
            holder.tv_content.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_add_new_service_item));
        }

        holder.itemView.setOnClickListener(v -> {
            ProcessNewBean.ServiceBaseShortBean shortBean = mList.get(position);
            mList.remove(position);
            mList.add(position,new ProcessNewBean.ServiceBaseShortBean(shortBean.serviceBaseName,shortBean.serviceBaseSn,!shortBean.selected));

            notifyDataSetChanged();
            if (iOnClickChoice != null) {
                iOnClickChoice.iOnClickChoiceContent(mList);
            }
        });
    }

    IOnClickChoice iOnClickChoice;

    public void setiOnClickChoice(IOnClickChoice iOnClickChoice) {
        this.iOnClickChoice = iOnClickChoice;
    }

    public interface IOnClickChoice {
        /**
         * 获取更改状态的所有数据
         */
        void iOnClickChoiceContent(List<ProcessNewBean.ServiceBaseShortBean> sList);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ConstructionProcessPlanViewHolder extends RecyclerView.ViewHolder {
        TextView tv_content;
        public ConstructionProcessPlanViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_content = itemView.findViewById(R.id.tv_content);
        }
    }
}
